var test = true;
var steps;
if(test) {
	steps = {
		costtime: 0,
		startTime: new Date(),
		step1: {
			state: -1, //-1尚未开始， 0开始了，执行计时、开始调度点亮， 1 停止计时、调度完成
			costtime: 0
		},
		step2: {
			state: -1, //-1尚未开始， 0开始了，执行计时、开始调度点亮， 1 停止计时、调度完成
			costtime: 0
		},
		step3: {
			state: -1, //-1尚未开始， 0开始发送通知， 1 查询是否全部接收  2查询是否全部已处理 3任务结束 
			alertnum: 0, //通知的人数
			receivenum: 0, //接收人数
			dealnum: 0, //处理的人数
			totalnum: 1, //总人数
			costtime: 0,
			taskList: [{
				"mainManId": "1",
				"mainManName": "	焦令军,夏志松",
				"issueContent": "环岛天桥客流橙色预警，有请焦令军和夏志松两位领导抵达现场进行指挥"
			}]
		},
		step4: {
			state: -1, //-1尚未开始， 0开始发送通知， 1 查询是否全部接收  2查询是否全部已处理  3任务结束 
			alertnum: 0, //通知的人数
			receivenum: 0, //接收人数
			dealnum: 0, //处理的人数
			totalnum: 1, //总人数
			costtime: 0,
			taskList: [{
				"mainManId": "1",
				"mainManName": "钱亚明",
				"issueContent": "环岛天桥客流橙色预警，请对观光拍照人员进行疏导"
			}]
		},
		step5: {
			state: -1, //-1尚未开始， 0开始发送通知， 1 查询是否全部接收  2查询是否全部已处理  3任务结束 
			alertnum: 0, //通知的人数
			receivenum: 0, //接收人数
			dealnum: 0, //处理的人数
			totalnum: 1, //总人数
			costtime: 0,
			taskList: [{
				"mainManId": "1",
				"mainManName": "夏志松",
				"issueContent": "环岛天桥客流橙色预警，请联系二层连廊负责人：徐国华，联系电话：137 6448 3467，请通知养护公司电梯单向向下运行",
				"litigantName": "徐国华",
				"litigantPhone": "13764483467"
			}]
		},
		step6: {
			state: -1, //-1尚未开始， 0开始发送通知， 1 查询是否全部接收  2查询是否全部已处理  3任务结束 
			alertnum: 0, //通知的人数
			receivenum: 0, //接收人数
			dealnum: 0, //处理的人数
			totalnum: 1, //总人数
			costtime: 0,
			taskList: [{
				"mainManId": "1",
				"mainManName": "Jason",
				"issueContent": "环岛天桥客流橙色预警，有请焦令君和夏志松抵达现场进行指挥"
			}]
		},
		step7: {
			state: -1, //-1尚未开始， 0 全部正常运转  1 执行人数下调操作   2任务结束
		}
	};
}
else{
	steps = {
		costtime: 0,
		startTime: new Date(),
		step1: {
			state: -1, //-1尚未开始， 0开始了，执行计时、开始调度点亮， 1 停止计时、调度完成
			costtime: 0
		},
		step2: {
			state: -1, //-1尚未开始， 0开始了，执行计时、开始调度点亮， 1 停止计时、调度完成
			costtime: 0
		},
		step3: {
			state: -1, //-1尚未开始， 0开始发送通知， 1 查询是否全部接收  2查询是否全部已处理 3任务结束 
			alertnum: 0, //通知的人数
			receivenum: 0, //接收人数
			dealnum: 0, //处理的人数
			totalnum: 5, //总人数
			costtime: 0,
			taskList: [{
				"mainManId": "370,371",
				"mainManName": "	焦令军,夏志松",
				"issueContent": "有请焦令军和夏志松两位领导抵达现场进行指挥"
			}, {
				"mainManId": "372,373,374",
				"mainManName": "	张翼飞,秦聚德,王奇为",
				"issueContent": "有请张翼飞、秦聚德、王奇三位领导进行综合指挥"
			}]
		},
		step4: {
			state: -1, //-1尚未开始， 0开始发送通知， 1 查询是否全部接收  2查询是否全部已处理  3任务结束 
			alertnum: 0, //通知的人数
			receivenum: 0, //接收人数
			dealnum: 0, //处理的人数
			totalnum: 9, //总人数
			costtime: 0,
			taskList: [{
				"mainManId":"287,288,289,290,291,292,293,294",
				"mainManName": "地铁增10A,地铁增10B,地铁增10C,地铁增10D,地铁增11A,地铁增11B,地铁增11C,地铁增11D",
				"issueContent": "在现场民警指挥下，对观光拍照人员进行疏导"
			}, {
				"mainManId": "371",
				"mainManName": "夏志松",
				"issueContent": "请通知摄影点撤摊，摄影点联系人：牛海峰，联系电话：137 6146 6457",
				"litigantName": "牛海峰",
				"litigantPhone": "13761466457"
			}]
		},
		step5: {
			state: -1, //-1尚未开始， 0开始发送通知， 1 查询是否全部接收  2查询是否全部已处理  3任务结束 
			alertnum: 0, //通知的人数
			receivenum: 0, //接收人数
			dealnum: 0, //处理的人数
			totalnum: 1, //总人数
			costtime: 0,
			taskList: [{
				"mainManId": "371",
				"mainManName": "夏志松",
				"issueContent": "请通知二层连廊负责人，联系养护公司，电梯单向向下运行：徐国华，联系电话：137 6448 3467",
				"litigantName": "徐国华",
				"litigantPhone": "13764483467"
			}]
		},
		step6: {
			state: -1, //-1尚未开始， 0开始发送通知， 1 查询是否全部接收  2查询是否全部已处理  3任务结束 
			alertnum: 0, //通知的人数
			receivenum: 0, //接收人数
			dealnum: 0, //处理的人数
			totalnum: 42, //总人数
			costtime: 0,
			taskList: [{
				"mainManId": "287,288,289,290,291,292,293,294",
				"mainManName": "地铁增10A,地铁增10B,地铁增10C,地铁增10D,地铁增11A,地铁增11B,地铁增11C,地铁增11D",
				"issueContent": "抵达麦当劳天桥通道口，协助民警进行现场封控"
			},{
				"mainManId": "312,313,235,236,241,242,243,244,245,246,247,256,257,258,259",
				"mainManName": "正大7A,正大7B,东方3A,东方3B,东方增1A,东方增1B,东方增2A,东方增2B,东方增2C,东方增3A,东方增3B,东方增4A,东方增4B,东方增5A,东方增5B",
				"issueContent": "抵达地铁一号口上口，协助民警进行现场封控"
			},{
				"mainManId": "314,315,335,336,337,338,339",
				"mainManName": "正大8A,正大8B,新三高增3A,新三高增3B,新三高增3C,新三高增3D,新三高增3E",
				"issueContent": "抵达正大广场出入口，协助民警进行现场封控"
			},{
				"mainManId": "302,303,304,305",
				"mainManName": "正大2A,正大2B,正大3A,正大3B",
				"issueContent": "抵达国金出入口，协助民警进行现场封控"
			},{
				"mainManId": "306,307",
				"mainManName": "正大4A,正大4B",
				"issueContent": "抵达明珠出入口，协助民警进行现场封控"
			},
			{
				"mainManId": "361,362,363,364",
				"mainManName": "山华,周学钢,杨虎,邱泽利",
				"issueContent": "抵达陆家嘴环岛区域进行现场封控"
			},{
				"mainManId": "365,366",
				"mainManName": "韩军,陈裕秋",
				"issueContent": "抵达陆家嘴环岛区域协助民警进行现场封控"
			}]
		},
		step7: {
			state: -1, //-1尚未开始， 0 全部正常运转  1 执行人数下调操作   2任务结束
		}
	};
}

var steps1;
steps1 =clone(steps);
var taskUserList = [];

function pushMainManIdToList(taskList) {
	for(var i = 0; i < taskList.length; i++) {
		var mainManIds = taskList[i].mainManId.split(",");
		for (var j=0; j<mainManIds.length; j++) {
			var userId = mainManIds[j];
			if(taskUserList.indexOf(userId) == -1){
				taskUserList.push(userId);
			}
		}
	}
}

pushMainManIdToList(steps.step3.taskList);
pushMainManIdToList(steps.step4.taskList);
pushMainManIdToList(steps.step5.taskList);
pushMainManIdToList(steps.step6.taskList);
//console.log("taskUserList:"+JSON.stringify(taskUserList));
//console.log("taskUserList:"+taskUserList.length);
/*
 * 任务3发送通知
 */
function step3StartPush() {
	var taskList = page.steps.step3.taskList;
	for(var i = 0; i < taskList.length; i++) {
		addData4genTask(taskList[i], i, function(state, data, index) {
			if(state) {
				page.steps.step3.state = 1;
				taskList[index] = data;
			}
		});
	}
}

/*
 * 任务3查询接收人数
 */
function step3CheckRecv() {
	var taskList = page.steps.step3.taskList;
	getTaskListNumber(taskList, function(state, result) {
		page.steps.step3.alertnum = result.number_total;
		page.steps.step3.receivenum = result.number_hasReceiveTask;
		page.steps.step3.dealnum = result.number_finish;
		page.steps.step3.totalnum = result.number_total;
		if(result.number_hasReceiveTask == result.number_total) {
			//当全部接收后
			page.steps.step3.state = 2;
		}
	});
}
/*
 * 任务3查询处理人数
 */
function step3CheckDeal() {
	var taskList = page.steps.step3.taskList;
	getTaskListNumber(taskList, function(state, result) {
		page.steps.step3.alertnum = result.number_total;
		page.steps.step3.receivenum = result.number_hasReceiveTask;
		page.steps.step3.dealnum = result.number_finish;
		page.steps.step3.totalnum = result.number_total;
		if(result.number_finish == result.number_total) {
			//当全部处理后，任务变成已完成
			page.steps.step3.state = 3;
		}
	});
}
/*
 * 任务4发送通知
 */
function step4StartPush() {
	var taskList = page.steps.step4.taskList;
	for(var i = 0; i < taskList.length; i++) {
		addData4genTask(taskList[i], i, function(state, data, index) {
			if(state) {
				page.steps.step4.state = 1;
				taskList[index] = data;
			}
		});
	}
}
/*
 * 任务4查询接收人数
 */
function step4CheckRecv() {
	var taskList = page.steps.step4.taskList;
	getTaskListNumber(taskList, function(state, result) {
		page.steps.step4.alertnum = result.number_total;
		page.steps.step4.receivenum = result.number_hasReceiveTask;
		page.steps.step4.dealnum = result.number_finish;
		page.steps.step4.totalnum = result.number_total;
		if(result.number_hasReceiveTask == result.number_total) {
			//当全部接收后
			page.steps.step4.state = 2;
		}
	});
}
/*
 * 任务4查询处理人数
 */
function step4CheckDeal() {
	var taskList = page.steps.step4.taskList;
	getTaskListNumber(taskList, function(state, result) {
		page.steps.step4.alertnum = result.number_total;
		page.steps.step4.receivenum = result.number_hasReceiveTask;
		page.steps.step4.dealnum = result.number_finish;
		page.steps.step4.totalnum = result.number_total;
		if(result.number_finish == result.number_total) {
			//当全部处理后，任务变成已完成
			page.steps.step4.state = 3;
		}
	});
}

/*
 * 任务5发送通知
 */
function step5StartPush() {
	var taskList = page.steps.step5.taskList;
	for(var i = 0; i < taskList.length; i++) {
		addData4genTask(taskList[i], i, function(state, data, index) {
			if(state) {
				page.steps.step5.state = 2;
				taskList[index] = data;
			}
		});
	}
}
/*
 * 任务5查询处理人数
 */
function step5CheckDeal() {
	var taskList = page.steps.step5.taskList;
	getTaskListNumber(taskList, function(state, result) {
		page.steps.step5.alertnum = result.number_total;
		page.steps.step5.receivenum = result.number_hasReceiveTask;
		page.steps.step5.dealnum = result.number_finish;
		page.steps.step5.totalnum = result.number_total;
		if(result.number_finish == result.number_total) {
			//当全部处理后，任务变成已完成
			page.steps.step5.state = 3;
		}
	});
}
/*
 * 任务6发送通知
 */
function step6StartPush() {
	var taskList = page.steps.step6.taskList;
	for(var i = 0; i < taskList.length; i++) {
		addData4genTask(taskList[i], i, function(state, data, index) {
			if(state) {
				page.steps.alertnum = page.steps.totalnum;
				page.steps.step6.state = 1;
				taskList[index] = data;
			}
		});
	}
}

/*
 * 任务6查询接收人数
 */
function step6CheckRecv() {
	var taskList = page.steps.step6.taskList;
	getTaskListNumber(taskList, function(state, result) {
		page.steps.step6.alertnum = result.number_total;
		page.steps.step6.receivenum = result.number_hasReceiveTask;
		page.steps.step6.dealnum = result.number_finish;
		page.steps.step6.totalnum = result.number_total;
		if(result.number_hasReceiveTask == result.number_total) {
			//当全部接收后
			page.steps.step6.state = 2;
		}
	});
}
/*
 * 任务6查询处理人数
 */
function step6CheckDeal() {
	var taskList = page.steps.step6.taskList;
	getTaskListNumber(taskList, function(state, result) {
		page.steps.step6.alertnum = result.number_total;
		page.steps.step6.receivenum = result.number_hasReceiveTask;
		page.steps.step6.dealnum = result.number_finish;
		page.steps.step6.totalnum = result.number_total;
		if(result.number_finish == result.number_total) {
			//当全部处理后，任务变成已完成
			page.steps.step6.state = 3;
		}
	});
}
/*
 * 任务7下调整人数
 */
function step7ReducePeople() {
	if(page.people>1999){
		page.dealPlace.peopleNum = (page.dealPlace.peopleNum - 50);
		if(page.dealPlace.peopleNum<1900){
			page.steps.step7.state = 2;
		}
	}
	else{
		var param = {
			"id": page.dealPlace.id,
			"peopleNum": (page.dealPlace.peopleNum - 50)
		};
		api.post(webUrl + "owTmpAction/updateOwTmpDisplayPeopleNum.mvc", param, function(data) {
			if(data.success) {
				if(data.resultObj) {
					page.dealPlace = data.resultObj;
					var peopleNum = parseInt(page.dealPlace.peopleNum);
					var alermOrange = parseInt(page.dealPlace.alermOrange);
					if(page.dealPlace.peopleNum < alermOrange - 10) {
						queryDisplayList();
					}
					if(page.dealPlace.peopleNum < alermOrange - 100) {
						page.steps.step7.state = 2;
						queryDisplayList();
					}
				}
			} else {}
		});	
	}
}

function getUrlParam(key) {
	var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
	var r = decodeURI(window.location.search).substr(1).match(reg);
	if(r != null) return unescape(r[2]);
	return "";
}

Array.prototype.indexOf = function(val) {
	for(var i = 0; i < this.length; i++) {
		if(this[i] == val) return i;
	}
	return -1;
};
Array.prototype.remove = function(val) {
	var index = this.indexOf(val);
	if(index > -1) {
		this.splice(index, 1);
	}
};

function clone(obj) {
    // Handle the 3 simple types, and null or undefined or function
    if (null == obj || "object" != typeof obj) return obj;

    // Handle Date
    if (obj instanceof Date) {
        var copy = new Date();
        copy.setTime(obj.getTime());
        return copy;
    }
    // Handle Array or Object
    if (obj instanceof Array | obj instanceof Object) {
        var copy = (obj instanceof Array)?[]:{};
        for (var attr in obj) {
            if (obj.hasOwnProperty(attr))
              copy[attr] = clone(obj[attr]);
        }
        return copy;
    }
    throw new Error("Unable to clone obj! Its type isn't supported.");
}

Date.prototype.format = function(fmt) {
	var o = {
		"M+": this.getMonth() + 1, //月份         
		"d+": this.getDate(), //日         
		"h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //小时         
		"H+": this.getHours(), //小时         
		"m+": this.getMinutes(), //分         
		"s+": this.getSeconds(), //秒         
		"q+": Math.floor((this.getMonth() + 3) / 3), //季度         
		"S": this.getMilliseconds() //毫秒         
	};
	var week = {
		"0": "/u65e5",
		"1": "/u4e00",
		"2": "/u4e8c",
		"3": "/u4e09",
		"4": "/u56db",
		"5": "/u4e94",
		"6": "/u516d"
	};
	if(/(y+)/.test(fmt)) {
		fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	}
	if(/(E+)/.test(fmt)) {
		fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "/u661f/u671f" : "/u5468") : "") + week[this.getDay() + ""]);
	}
	for(var k in o) {
		if(new RegExp("(" + k + ")").test(fmt)) {
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
		}
	}
	return fmt;
}

var api = {
	post: function(url, param, responseCallback) {
		mui.ajax({
			url: url,
			data: param,
			async: true, // 异步 || 同步
			dataType: 'json',
			type: 'post',
			timeout: 10000,
			success: function(data) {
				// 请求成功
				//				console.log("reply:" + JSON.stringify(data));
				responseCallback(data);
			},
			error: function(xhr, type, errorThrown) {
				// 请求失败
				responseCallback({
					"success": false,
					"content": "网络不给力"
				});
			}
		});
	},
	get: function(url, responseCallback) {
		mui.ajax({
			url: url,
			data: param,
			async: true, // 异步 || 同步
			dataType: 'json',
			type: 'get',
			timeout: 10000,
			success: function(data) {
				// 请求成功
				console.log("reply:" + JSON.stringify(data));
				responseCallback(data);
			},
			error: function(xhr, type, errorThrown) {
				// 请求失败
				responseCallback({
					"success": false,
					"content": "网络不给力"
				});
			}
		});
	}
}

/*
 * 添加任务
 */
function addData4genTask(param, index, callback) {
	param.registerDataNumber = "001";
	param.taskNumber = "001";
	param.name = "环岛天桥客流橙色预警";
	param.owPriorityCode = "1";
	param.owTaskType = "1";
	param.owRegisterType = "1";
	param.owViolationType = "1";
	param.finderDeptId = "1";
	param.finderDeptName = "统计部门";
	param.baseCommiteeId = "1";
	param.baseRoadId = "1";
	param.posHouseNumber = "1";
	param.posDesc = "陆家嘴环岛天桥";
	param.posLng = "121.507177";
	param.posLat = "31.243885";
	param.baseRoadId = "1";
	param.posHouseNumber = "1";
	param.posDesc = "陆家嘴环岛天桥";
	if(!param.hasOwnProperty('litigantName')) {
		param.litigantName = "无";
	}
	if(!param.hasOwnProperty('litigantPhone')) {
		param.litigantPhone = "";
	}
	param.assistManId = "";
	param.assistManName = "";
	param.remark = param.issueContent;
	param.improveRequied = "";
	param.dealIssueContent = "人流密集";
	param.owDealType = "1";
	param.dealTime = "";
	param.dealResult = "";
	param.dealRemark = "";
	param.CreatemanId = "0";
	param.createman = "tmp-admin";
	param.companyId = "1";
	var index1 = index;
	api.post(webUrl + "owTmpAction/addData4genTask.mvc", param, function(data) {
		if(data.success) {
			if(data.resultObj) {
				param = data.resultObj;
				callback(true, data.resultObj, index1);
			}
		} else {
			setTimeout(function() {
				addData4genTask(param, index1, callback);
			}, 3000);
		}
	});
}

/*
 * 查询任务接收情况
 */
function queryClaimTaskNumber(param, callback) {
	api.post(webUrl + "owTmpAction/queryClaimTaskNumber.mvc", param, function(data) {
		if(data.success) {
			if(data.resultMap) {
				callback(true, data.resultMap);
			}
		} else {
			callback(false, "查询失败");
		}
	});
}
/*
 * 查询remark,触发任务结束
 */
function queryRemark() {
	var param = {};
	api.post(webUrl + "owTmpAction/queryDisplayList.mvc", param, function(data) {
		if(data.success) {
			if(data.resultMap) {
				var list = data.resultMap.rows;
				for(var i = 0; i < list.length; i++) {
					if(page.dealPlace.id == list[i].id) {
						if(page.controlRemark != list[i].remark) {
							page.controlRemark = list[i].remark;
							page.steps.step1.state = 1;
							page.steps.step2.state = 1;
							page.steps.step3.state = 3;
							page.steps.step4.state = 3;
							page.steps.step5.state = 3;
							page.steps.step6.state = 3;
							page.steps.step7.state = 0;
							page.steps.step6.alertnum = page.steps.step6.totalnum; //通知的人数
							page.steps.step6.receivenum = page.steps.step6.totalnum; //接收人数
							page.steps.step6.dealnum = page.steps.step6.totalnum; //处理的人数
							page.steps.step5.alertnum = page.steps.step5.totalnum; //通知的人数
							page.steps.step5.receivenum = page.steps.step5.totalnum; //接收人数
							page.steps.step5.dealnum = page.steps.step5.totalnum; //处理的人数
							page.steps.step4.alertnum = page.steps.step4.totalnum; //通知的人数
							page.steps.step4.receivenum = page.steps.step4.totalnum; //接收人数
							page.steps.step4.dealnum = page.steps.step4.totalnum; //处理的人数
							page.steps.step3.alertnum = page.steps.step3.totalnum; //通知的人数
							page.steps.step3.receivenum = page.steps.step3.totalnum; //接收人数
							page.steps.step3.dealnum = page.steps.step3.totalnum; //处理的人数
							
						}
					}
				}
			}
		} else {}
	});
}

/*
 * 查询需要展示的数据
 */
function queryDisplayList() {
	var param = {};
	api.post(webUrl + "owTmpAction/queryDisplayList.mvc", param, function(data) {
		if(data.success) {
			if(data.resultMap) {
				page.placeList = data.resultMap.rows;
				for(var i = 0; i < page.placeList.length; i++) {
					var place = page.placeList[i];
					var peopleNum = parseInt(place.peopleNum);
					//wxl新加
					if(place.id=="7" && page.people>2000){
						peopleNum = page.people;
						place.peopleNum = page.people;
					}
					
					var alermOrange = parseInt(place.alermOrange);
					if(peopleNum >= alermOrange) {
						//触发了橙色预警
						page.controlState = 2;
						page.dealPlace = place;
						page.controlRemark = place.remark;
						page.steps = clone(steps);
						page.steps.startTime = new Date();
						break;
					}
				}
			}
		} else {}
	});
}

/*
 * 查询任务状态
 */
function getTaskListNumber(taskList, callback) {
	var result = {
		taskDeal: 0,
		number_hasReceiveTask: 0,
		number_total: 0,
		number_finish: 0
	}
	for(var i = 0; i < taskList.length; i++) {
		var taskIndex = i;
		var param = {
			"owTaskId": taskList[i].id
		}
		queryClaimTaskNumber(param, function(state, data) {
			result.taskDeal++;
			if(state) {
				result.number_hasReceiveTask += data.number_hasReceiveTask;
				result.number_finish += data.number_finish;
				result.number_total += data.number_total;
			} else {
				result.number_total++;
			}
			if(result.taskDeal == taskList.length) {
				callback(true, result);
			}
		});
	}
}

/*
 * 获取最近5分钟的路径数据，实时控制位置
 */
function dynamicLocation() {
	var now = new Date();
	var start = new Date(now.getTime() - 2 * 60 * 1000);
	var param = {
		"createdate_Start": start.format("yyyy-MM-dd HH:mm:ss"),
		"createdate_End": now.format("yyyy-MM-dd HH:mm:ss"),
		"sortOrder": "desc"
	};
	api.post(webUrl + "owTmpAction/queryUserTrackList.mvc", param, function(data) {
		if(data.success) {
			if(data.resultMap && data.resultMap.rows) {
				var rows = data.resultMap.rows;
				page.usersLocationflag++;
				for(var i = 0; i < rows.length; i++) {
					var row = rows[i];
					if(page.usersMap.hasOwnProperty(row.a2userId)) {
						var user = page.usersMap[row.a2userId];
						if(user.usersLocationflag != page.usersLocationflag) {
							user.usersLocationflag = page.usersLocationflag;
							addUserToMap(user, row.lat, row.lng);
						}
					}
				}
				var overlays = map.getOverlays();
				for(var i = 0; i < overlays.length; i++) {
					if(overlays[i].hasOwnProperty("userId") && overlays[i].userId != "-1") {
						if(overlays[i].usersLocationflag != page.usersLocationflag) {
							overlays[i].hide();
						}
					}
				}
			}
		} else {}
	});
}

/*
 * 查询并且规划轨迹
 */
function queryAndDarwPath() {
	for(var i = 0; i < taskUserList.length; i++) {
		var user = page.usersMap[taskUserList[i]];
		queryAndDarwPathByUser(user);
	}
}
function getDisanceByCoords(coords1, coords2) {
	return getDisance(coords1.latitude, coords1.longitude, coords2.latitude, coords2.longitude);
}
function toRad(d) {
	return d * Math.PI / 180;
}
/*
 * 返回单位是距离米
 */
function getDisance(lat1, lng1, lat2, lng2) { //#lat为纬度, lng为经度, 一定不要弄错
	var dis = 0;
	var radLat1 = toRad(lat1);
	var radLat2 = toRad(lat2);
	var deltaLat = radLat1 - radLat2;
	var deltaLng = toRad(lng1) - toRad(lng2);
	var dis = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(deltaLat / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(deltaLng / 2), 2)));
	return dis * 6378137;
}

function queryAndDarwPathByUser(user) {
	if(!user) {
		return;
	}
	if(!user.hasOwnProperty("startTime")) {
		user.startTime = page.steps.startTime;
	}
	if(user.startTime.getTime() < page.steps.startTime.getTime()) {
		user.startTime = page.steps.startTime;
	}
	var param = {
		"a2userId": user.id,
		"createdate_Start": user.startTime.format("yyyy-MM-dd HH:mm:ss"),
		"createdate_End": (new Date()).format("yyyy-MM-dd HH:mm:ss"),
		"sortOrder": "asc"
	};
	api.post(webUrl + "owTmpAction/queryUserTrackList.mvc", param, function(data) {
		if(data.success) {
			if(data.resultMap && data.resultMap.rows && data.resultMap.rows.length > 2) {
				var rows = data.resultMap.rows;
				var points = [];
				for(var i = 0; i < rows.length; i++) {
					var point = new BMap.Point(parseFloat(rows[i].lng), parseFloat(rows[i].lat));
					if(getDisanceByCoords({latitude:31.240813, longitude:121.520266},{latitude:rows[i].lat, longitude:parseFloat(rows[i].lng)})<10000.0){
						points.push(point);
					}
				}
				user.startTime = new Date();
				if(user.hasOwnProperty("polyline") && user.polyline != null) {
					//有轨迹了，则增加轨迹
					var oldPoints = user.polyline.getPath();
					oldPoints = oldPoints.concat(points);
					user.polyline.setPath(oldPoints);
				} else {
					var strokeColor = "#e99a01";
					if(user.a2postName == "领导") {
						strokeColor = "#be0022";
					}
					else if(user.a2postName == "民警"){
						strokeColor = "#333333";
					}
					else if(user.a2postName == "城管"){
						strokeColor = "#13c6fe";
					}
					else if(user.a2postName == "交管") {
						strokeColor = "#81007f";
					}
					
					var polyline = new BMap.Polyline(points, {
						strokeColor: strokeColor,
						strokeWeight: 3,
						strokeOpacity: 0.7
					});
					map.addOverlay(polyline);
					user.polyline = polyline;
				}
			}
		} else {}
	});
}

function changeStr(value) {
	if(value == undefined || value == null) {
		return "";
	}
	return value;
}
/*
 * 在地图上添加用户
 */
function addUserToMap(user, lat, lng) {
	var point = new BMap.Point(parseFloat(lng), parseFloat(lat));
	if(user.marker) {
		user.marker.show();
		user.marker.usersLocationflag = user.usersLocationflag;
		user.marker.setPosition(point);
		return;
	}

	var labelColor = "#e99a01";
	var faceImage = "tebao.png";
	var bgImage = "orange_bg.png";
	if(user.a2postName == "民警") {
		faceImage = "minjing.png";
		bgImage = "black_bg.png";
		labelColor = "#333333";
	} else if(user.a2postName == "城管") {
		faceImage = "chengguan.png";
		bgImage = "blue_bg.png";
		labelColor = "#13c6fe";
	} else if(user.a2postName == "领导") {
		faceImage = "lingdao.png";
		bgImage = "red_bg.png";
		labelColor = "#be0022";
	} else if(user.a2postName == "交管") {
		faceImage = "jiaojin.png";
		bgImage = "purple_bg.png";
		labelColor = "#81007f";
	}

	var myIcon = new BMap.Icon(faceImage, new BMap.Size(14, 14), {
		anchor: new BMap.Size(8, 21)
	});
	myIcon.setImageSize(new BMap.Size(14, 14));
	//创建marker对象
	var marker = new BMap.Marker(point, {
		icon: myIcon
	});
	var shadowIcon = new BMap.Icon(bgImage, new BMap.Size(18, 24), {
		anchor: new BMap.Size(10, 24)
	});
	shadowIcon.setImageSize(new BMap.Size(18, 24));
	marker.setShadow(shadowIcon);
	marker.usersLocationflag = user.usersLocationflag;
	if(taskUserList.indexOf(user.id) != -1) {
		var label = new BMap.Label(user.name, {
			offset: new BMap.Size(-20, 15)
		});
		marker.setLabel(label);
		marker.userId = user.id;
		marker.user = user;
		label.setStyle({
			color: labelColor,
			fontSize: "12px",
			fontFamily: "微软雅黑",
			border: "none",
			borderRadius: "0px",
			padding: "0px",
			backgroundColor: "rgba(0,0,0,0)"
		});
	}
	marker.setAnimation(BMAP_ANIMATION_DROP);
	var opts = {
		width: 200, // 信息窗口宽度
		height: 130, // 信息窗口高度
		title: changeStr(user.name), // 信息窗口标题
		enableMessage: false, //设置允许信息窗发送短息
		message: ""
	}
	var messaege = "<span>岗位：</span>" + changeStr(user.a2postName) + "<br/><span>区域：</span>" + changeStr(user.jobNumber) + "<br/><span>岗点：</span>" + changeStr(user.idcardno) + "<br/><span>职责：</span>" + changeStr(user.telephone);
	var infoWindow = new BMap.InfoWindow(messaege, opts); // 创建信息窗口对象 
	marker.addEventListener("click", function() {
		map.openInfoWindow(infoWindow, marker.getPosition()); //开启信息窗口
	});

	marker.userId = user.id;
	user.marker = marker;
	map.addOverlay(marker);
}

/*
 * 获取用户列表
 */
function getUserList() {
	var param = {};
	api.post(webUrl + "owTmpAction/queryUsersFormat.mvc", param, function(data) {
		if(data.success) {
			if(data.resultMap && data.resultMap.rows) {
				page.usersMap = data.resultMap.rows;

				//				taskUserList
			} else {
				setTimeout(function() {
					getUserList();
				}, 3000);
			}
		} else {
			setTimeout(function() {
				getUserList();
			}, 3000);
		}
	});
}